<template>
    <!-- <ElDialog v-model="dialogVisible" width="600px" title="人工干预"> -->
    <UiForm :formData="formData" label-width="300px" @callback="submitForm" @close="close" />
    <!-- </ElDialog> -->
</template>
<script setup>
import { ElDialog, ElMessage } from "element-plus";
import UiForm from "@/components/uiForm.vue";
import { computed, ref, reactive, onMounted } from "vue";
import { channelStatus } from "@/utils/enum.js";
import { cascaderAddress } from "@/utils/addressData";
import { setChannel } from "@/server/manage/channel.js";
import { isNum, isNumReq } from "@/utils/validate";
import {
    getOrderEnterInfo,
    saveOrderEnterInfo,
    updateOrderEnterInfo,
} from "@/server/manage/incoming.js";

const prop = defineProps(["rowInfo"]);
const emit = defineEmits(["close", "callback"]);
// const dialogVisible = ref(true)
const platformData = ref([
    {
        label: "抖音",
        value: "DY",
    },
    {
        label: "快手",
        value: "KS",
    },
    {
        label: "京东",
        value: "JD",
    },
    {
        label: "淘宝",
        value: "TB",
    },
    {
        label: "天猫",
        value: "TM",
    },
    {
        label: "拼多多",
        value: "PDD",
    },
    {
        label: "小红书",
        value: "XHS",
    },
    {
        label: "其他",
        value: "QT",
    },
]);
const formData = reactive([
    {
        label: "企业名称：",
        type: "input",
        key: "companyName",
        value: "",
        disabled: true,
        labelWidith: "200px",
    },
    {
        label: "统一社会信用编码：",
        type: "input",
        key: "identityNo",
        value: "",
        labelWidith: "200px",
        disabled: true,
    },
    {
        label: "公司所在省市：",
        type: "cascader",
        key: "address1",
        value: "",
        required: true,
        cascaderOptions: cascaderAddress(),
        labelWidith: "200px",
    },
    {
        label: "公司成立年限(单位:月)：",
        type: "input",
        key: "months",
        value: "",
        required: true,
        labelWidith: "200px",
        rules: { validator: isNumReq, trigger: "blur" },
    },
    {
        label: "店铺平台：",
        type: "select",
        key: "ebusinessPlatform",
        value: "",
        required: true,
        labelWidith: "200px",
        options: platformData,
    },
    {
        label: "店铺开店时间：",
        type: "datePicker",
        key: "cooperateStarDate",
        widgetType: "",
        value: "",
        required: true,
        labelWidith: "200px",
    },
    {
        label: "近6个月累计销售额(不包含退款额)：",
        type: "input",
        key: "halfYearSaleAmount",
        value: "",
        required: true,
        labelWidith: "269px",
        rules: { validator: isNumReq, trigger: "blur" },
    },
    {
        label: "近12个月累计销售额(不包含退款额)：",
        type: "input",
        key: "lastYearSaleAmount",
        value: "",
        required: true,
        labelWidith: "269px",
        rules: { validator: isNumReq, trigger: "blur" },
    },
    {
        label: "近6个月累计退款额：",
        type: "input",
        key: "halfYearRefundAmount",
        value: "",
        labelWidith: "200px",
        rules: { validator: isNum, trigger: "blur" },
    },
    {
        label: "近12个月累计退款额：",
        type: "input",
        key: "lastYearRefundAmount",
        value: "",
        labelWidith: "200px",
        rules: { validator: isNum, trigger: "blur" },
    },
    {
        label: "近1个月的应收货款总额：",
        type: "input",
        key: "oneMonthReceivableAmount",
        value: "",
        labelWidith: "200px",
        rules: { validator: isNum, trigger: "blur" },
    },
    {
        label: "近1个月的应退款总额：",
        type: "input",
        key: "oneMonthRefundAmount",
        value: "",
        labelWidith: "200px",
        rules: { validator: isNum, trigger: "blur" },
    },
]);
const id = ref();
// 提交
const submitForm = (formData) => {
    if (type.value == "update") {
        let parmas = {
            orderNum: prop.rowInfo.orderNum,
            id: id.value,
            address: formData.address1[0] + "-" + formData.address1[1],
            ...formData,
        };
        updateOrderEnterInfo(parmas)
            .then((res) => {
                emit("close");
            })
            .catch((msg) => {
                emit("close");
            });
    } else {
        let parmas = {
            orderNum: prop.rowInfo.orderNum,
            address: formData.address1[0] + "-" + formData.address1[1],
            ...formData,
        };
        saveOrderEnterInfo(parmas)
            .then((res) => {
                emit("close");
            })
            .catch((msg) => {
                emit("close");
            });
    }
};
// 取消
const close = () => {
    emit("close");
};

//   emit('callback', paramsData(props.formData))
const type = ref("");
// 获取详情
const getDetail = () => {
    type.value = prop.rowInfo.type;
    if (type.value == "update") {
        getOrderEnterInfo({ orderNum: prop.rowInfo.orderNum })
            .then((res) => {
                // params.id = props.info.id;
                id.value = res.enterpriseOperateData.id;
                formData.forEach((item) => {
                    item.value = res.enterpriseOperateData[item.key];
                    if (item.key == "address1") {
                        item.value =
                            res.enterpriseOperateData.address.split("-");
                    }
                });
            })
            .catch((msg) => {});
    } else {
        formData.identityNo = prop.rowInfo.idNo;
        formData.ebusinessPlatform = prop.rowInfo.ebusinessPlatform;
        formData.forEach((item) => {
            if (item.key == "companyName") {
                item.value = prop.rowInfo.custName;
            }
            if (item.key == "identityNo") {
                item.value = prop.rowInfo.idNo;
            }
            if (item.key == "ebusinessPlatform") {
                item.value = prop.rowInfo.ebusinessPlatform;
            }
        });
    }
};
getDetail();
// onMounted(() => {
//     getDetail()
// })
</script>